<template>
	<view>
		<my-search @click.native="gotoSearch"></my-search>
		<view class="scroll-view-container">
			<scroll-view :scroll-y="true" class="left-box" :style="{ height: sh + 'px' }">
				<view class="scroll-item" v-for="(item, i) in cateList" :key="i" :class="i === active ? 'active' : ''"
					@click="handleChangeActive(i)">
					{{ item.cat_name }}
				</view>
			</scroll-view>

			<scroll-view scroll-y="true" class="right-box" :style="{ height: sh + 'px' }" :scroll-top="scrollTop">
				<view class="scroll-item" v-for="(item2,i) in cateLevel2" :key="i">
					<view class="title">/ {{item2.cat_name}} /</view>
					<!-- 三级类别 -->
					<view class="cate-lv3-list">
						<view class="cate-lv3-item" v-for="(item3, i3) in item2.children" :key="i3"
							@click="gotoGoodsList(item3)">
							<!-- 图片 -->
							<image :src="item3.cat_icon"></image>
							<!-- 文本 -->
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from "@/mixins/tab-bar-badge.js"
	export default {
		mixins: [badgeMix],
		data() {
			return {
				sh: 0, //scroll-height
				cateList: [],
				active: 0,
				cateLevel2: [],
				cateLevel3: [],
				scrollTop: 0
			};
		},
		methods: {
			async getCateList() {
				const url = 'api/public/v1/categories';
				const {
					data
				} = await uni.$http.get(url);
				if (data.meta.status !== 200) return uni.$showMsg();
				this.cateList = data.message;

				this.cateLevel2 = this.cateList[this.active].children
			},

			handleChangeActive(index) {
				this.active = index;

				this.cateLevel2 = this.cateList[index].children
				// 都是复用一个页面, 在点击Tabs时更改位置
				this.scrollTop = this.scrollTop ? 0 : 0.1
			},

			gotoGoodsList(item3) {
				uni.navigateTo({
					url: "/subpkg/goods_list/goods_list?cid=" + item3.cat_id
				})
			},

			gotoSearch() {
				uni.navigateTo({
					url:"/subpkg/search/search"
				})
			},
		},
		onLoad() {
			// 设置屏幕高度,减去导航栏高度
			const info = uni.getSystemInfoSync();
			this.sh = info.windowHeight - 50;

			this.getCateList();
		},

	};
</script>

<style scoped lang="less">
	.scroll-view-container {
		display: flex;

		.left-box {
			width: 120px;

			.scroll-item {
				line-height: 60px;
				background-color: #f7f7f7;
				text-align: center;
				font-size: 12px;
			}

			& .active {
				background-color: #ffffff;
				position: relative;

				&::before {
					content: '';
					display: block;
					height: 35px;
					width: 2px;
					background-color: red;
					position: absolute;
					top: 50%;
					left: 0;
					transform: translateY(-50%);
				}
			}
		}

		.right-box {
			.title {
				font-size: 12px;
				font-weight: bold;
				text-align: center;
				padding: 15px 0
			}

			.cate-lv3-list {
				display: flex;
				flex-wrap: wrap;

				.cate-lv3-item {
					width: 33.33%;
					margin-bottom: 10px;
					display: flex;
					flex-direction: column;
					align-items: center;

					image {
						width: 60px;
						height: 60px;
					}

					text {
						font-size: 12px;
					}
				}
			}
		}
	}
</style>
